package com.lagou.mr.homework;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.CombineTextInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class HomeworkDriver {
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {

        //1 获取配置信息及封装任务
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf, "HomeworkDirver");

        //2 设置jar加载路径
        job.setJarByClass(HomeworkDriver.class);

        //3 设置map和reduce类
        job.setMapperClass(HomeworkMapper.class);
        job.setReducerClass(HomeworkReducer.class);

        //4 设置map输出
        job.setMapOutputKeyClass(IntWritable.class);
        job.setMapOutputValueClass(NullWritable.class);

        //5 设置最终输出的kv类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(NullWritable.class);

        //6 设置输入类
        job.setInputFormatClass(CombineTextInputFormat.class);

        //7 设置reducetask 数量
        job.setNumReduceTasks(1);

        //7 设置输入和输出路径
        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        //8 提交
        boolean result = job.waitForCompletion(true);

        System.exit(result ? 0 : 1);
    }
}
